<?php

namespace backend\controllers\index;

use backend\controllers\BaseController;
use backend\models\system\Admin;
use backend\models\Picture;
use backend\models\UploadForm;
use Yii;
use yii\db\Exception;
use yii\web\Response;
use yii\web\UploadedFile;

/**
 * 后台首页控制器
 * @author longfei <phphome@qq.com>
 */
class IndexController extends BaseController
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionConsole()
    {
        return $this->render('console');
    }

    /**
     * 修改密码
     * @return string
     */
    public function actionUpdatePassword()
    {

        $model = Admin::findOne(Yii::$app->user->identity->getId());
        $model->scenario = Admin::SCENARIOS_UPDATE_PASSWORD;

        $return = ['status' => 0, 'msg' => '非法操作'];
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $post = Yii::$app->request->post('Admin', '');
                if(empty($post['password']) || empty($post['new_password'])) {
                    throw new Exception('新旧密码都不能为空');
                }


                if(!$model->validatePassword($post['password'])) {
                    throw new Exception('旧密码错误');
                }

                if (strlen($post['new_password']) < 6) {
                    throw new Exception('密码不能小于6个字符');
                }

                if($post['password'] == $post['new_password']) {
                    throw new Exception('新密码不能和旧密码相同');
                }

                $model->setPassword($post['new_password']);
                $model->new_password = $model->password;
                if (!$model->save()) {
                    throw new Exception('修改失败：'.array_values($model->getFirstErrors())[0]);
                }


                $return['status'] = 1;
                $return['msg'] = '修改成功';
            } catch (Exception $e) {
                $return['msg'] = $e->getMessage();
            }

            return $return;
        }

        return $this->render('update-password', [
            'model' => $model,
        ]);
    }

    public function actionUpload()
    {
        if (Yii::$app->request->isPost) {
            // 批量存入文件和写入文件信息
            $model = new UploadForm();
            $model->files = UploadedFile::getInstancesByName('down_url');   // 从$_FILES中获取附件文件
            if (empty($model->files)) {
                $this->error('未找到上传的文件!');
            }
            // 上传
            $module = 'premise';
            $res = $model->upload(['module' => $module, 'is_thumbnail' => 0]);

            if (!$res['status']) {
                $this->error('上传失败!');
            }

            foreach ($res['msg'] as $key => $value) {
                $saveRes = Picture::savePic($value['file_url']);
                unset($res['msg'][$key]['tmp_name']);
            }

            return $this->redirect(['upload']);

        }

        $picList = Picture::find()->where(['status' => 1])->all();
        return $this->render('upload', ['picList' => $picList]);
    }

}
